/*
Copyright  2018 Blockchain Technology and Application Joint Lab, Fintech Research Center of ISCAS.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */
import Dexie from 'dexie';
import {addAdminItem, addcopyrights, addUserItem} from './fake'
import {addcopyrightsToMongo} from './fake'
//申请新的对象，并创建数据库
const db = new Dexie('crbb');

//创建图片版权证书表
db.version(1).stores({
    // copyright_certificates: '++id, &certificate_number, owner_account_address, status, block_timestamp, hash_algorithm, picture_digital_identity, picture_desc, createdAt',
    users: '++id, &phone, prv_key_pem_encrypted, user_certificate_pem, address, unit, account_address, credential_number, name, email, createdAt, status, avatar, isAdmin',
    user_cert: '++id, &user_tel, serial_number, signature_algorithm, issuer, not_before, not_after, subject, key_algorithm, signature',
    // register_user: '++id, &phone, name, email, unit, address, createdAt, status, &account_address, user_certificate_pem, avatar'
});
//add test data
db.users.count().then((count) => {
    if (count == 0) {
        addAdminItem();
        addUserItem();
    }
})

//fake copyrights if it is null
Meteor.call('certificate.count',(err, res)=>{
    if(res == 0){
        addcopyrightsToMongo();
    }
    if(err){
        console.log(err)
    }
})

export default db;